# -*-coding:utf-8 -*-
"""
@project: gnss
@authoe:  ybad
@time:    2019-07-25 11-48-35
@email:   https://gitee.com/yjfgitee
"""
from math import sin, radians, fabs, asin, cos, sqrt


class GPSDistanceCalculation(object):

    def __init__(self):
        self.earth_radius = 6371.393

    def hav(self, theta):
        s = sin(theta / 2)
        return s * s

    def get_distance_hav(self, lng0: str, lat0: str, lng1: str, lat1: str):
        "用haversine公式计算球面两点间的距离。"
        # 经纬度转换成弧度
        lat0 = radians(float(lat0))
        lat1 = radians(float(lat1))
        lng0 = radians(float(lng0))
        lng1 = radians(float(lng1))

        dlng = fabs(lng0 - lng1)
        dlat = fabs(lat0 - lat1)
        h = self.hav(dlat) + cos(lat0) * cos(lat1) * self.hav(dlng)
        distance = 2 * self.earth_radius * asin(sqrt(h))

        return distance

    # 计算距离
    # """
    #     sqet:[{"longitude":"","latitude":""},{"longitude":"","latitude":""},......]
    # """
    #
    # def calculated_distance(self, sqet):
    #     length = 0
    #     if len(sqet) <= 1:
    #         return length
    #     for sq in range(len(sqet)-1):
    #         # 之所以不用sq来计算索引,是为了避免车辆行使时,返回原路的情况.
    #         length += self.get_distance_hav(sqet[sq]['longitude'], sqet[sq]['latitude'], sqet[sq+1]['longitude'], sqet[sq+1]['latitude'])
    #     return round(float(length), 3)
